﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Longbow.Security.Cryptography;
using Bootstrap.Security;
using PetaPoco;
using Longbow.Web.Mvc;

namespace Bootstrap.DataAccess.Model
{
    /// <summary>
    /// agentstatuslog表实体类
    /// </summary>
    [TableName("agentstatuslog")]
    public class AgentStatusStatistics
    {
        public string ID { get; set; }
        public string StatisticDate { get; set; } //时间
        public string Status { get; set; }//状态
        public string PhoneNo { get; set; } //分机
        public string AgentNo { get; set; } //座席
        public string StatisticTime { get; set; } //状态持续时间
        public string CallID { get; set; } //callid
        public string CallerID { get; set; } //主叫号
        public string CalleeID { get; set; }//被叫号
        public string CallDir { get; set; }//呼叫方向
        public string CallType { get; set; }//呼叫类型、区分内部通话和外部通话
        public string parkcause { get; set; }//小修/挂机理由 小休状态：小休理由;话后处理：挂机理由
        public string PrivateData { get; set; }//随路
        public string Date { get; set; } //日期
        public string EndTime { get; set; } //开始时间
        public string BeginTime { get; set; } //结束时间



        //查询座席状态统计sql
        private string _sql = "SELECT *,left(StatisticDate, 10) as Date,right(DATE_ADD(StatisticDate,INTERVAL StatisticTime SECOND),8) as EndTime,right(StatisticDate,8) as BeginTime FROM agentstatuslog t ";
        /// <summary>
        /// 查询座席状态统计
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public virtual Page<AgentStatusStatistics> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string CallerID, string CalleeID, string AgentNo, string CallDir)
        {
            var sql = new Sql(_sql);
            if (startTime.HasValue) sql.Where("StatisticDate >= @0", startTime.Value);
            if (endTime.HasValue) sql.Where("StatisticDate < @0", endTime.Value.AddDays(1).AddSeconds(-1));
            //if (startTime == null && endTime == null) sql.Where("StatisticDate > @0", DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod()));
            if (!string.IsNullOrEmpty(CallerID)) sql.Where("CallerID like '%" + CallerID + "%'");//主叫号
            if (!string.IsNullOrEmpty(CalleeID)) sql.Where("CalleeID like '%" + CalleeID + "%'");//被叫号
            if (!string.IsNullOrEmpty(AgentNo)) sql.Where("AgentNo like '%" + AgentNo + "%'");//座席号
            if (!string.IsNullOrEmpty(CallDir)) sql.Where("CallDir like '%" + CallDir + "%'");//呼叫方向
            //sql.Where("status=N'通话'");
            sql.OrderBy($"{po.Sort} {po.Order}");

            return DbManager.Create().Page<AgentStatusStatistics>(po.PageIndex, po.Limit, sql);
        }


    }
}
